package com.are.odisea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ManejadorPermisosRolNot {
	private db conexion = null;
	private PermisosRolNot pr;
	
	public static final int ORDER_ROL = 1;
	public static final int ORDER_DOC = 2;
	
	public ManejadorPermisosRolNot(db conexion) {
		super();
		this.setConexion(conexion);
		this.setPr(new PermisosRolNot());
	}
	public ManejadorPermisosRolNot(db conexion, PermisosRolNot pr) {
		super();
		this.setConexion(conexion);
		this.setPr(pr);
	}
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public PermisosRolNot getPr() {
		return pr;
	}
	public void setPr(PermisosRolNot pr) {
		this.pr = pr;
	}
	
	public boolean add() throws SQLException{
		boolean result = false;
			String sql = "insert into rol_noticia(rol,id_not) values (?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, pr.getRol());
			pst.setString(2, pr.getIdnot());
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	public boolean update(String key, String key2) throws SQLException{
		boolean result = false;

			String sql = "update rol_noticia set rol= ?, id_not=? where rol=? and id_not=?";
			
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, pr.getRol());
			pst.setString(2, pr.getIdnot());
			pst.setString(3, key);
			pst.setString(4, key2);
			
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			
		return result;
	}
	public boolean remove(String key, String key2) throws SQLException {
		boolean result = false;

			String sql = "delete from rol_noticia where rol=? and id_not=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			pst.setString(2,key2);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;	
	}
	
	public boolean exist(String key, String key2) throws SQLException {
		boolean result = false;

			String sql = "select rol,id_not from rol_noticia where rol = ? and id_not=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			pst.setString(2,key2);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public boolean Find(String key, String key2) throws SQLException {
		boolean result = false;

			String sql = "select rol, id_not from rol_noticia where rol=? and id_not=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				pr = new PermisosRolNot();
				pr.setRol(rs.getString("rol"));
				pr.setIdnot(rs.getString("id_not"));
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public ArrayList<PermisosRolNot> list(int orden, String Id_not) throws SQLException {
		ArrayList<PermisosRolNot> pr = new ArrayList<PermisosRolNot>();
		String sql ="";
		if(Id_not.equals("")){
				 sql = "select * from vrol_noticia";
			}else{
				 sql = "select * from vrol_noticia where id_not="+Id_not;
			}
			switch(orden) {
			case ORDER_ROL:
				sql += " order by rol";
			break;
			case ORDER_DOC:
				sql += " order by id_not";
			break;
			}
			ResultSet rs  = conexion.Query(sql);
			while(rs.next()) {
				PermisosRolNot p = new PermisosRolNot();
				p.setRol(rs.getString("rol"));
				p.setIdnot(rs.getString("Id_not"));
				p.setDesRol(rs.getString("DesRol"));
				p.setTitnot(rs.getString("titnot"));
				pr.add(p);
			}
			rs.close();
		return pr;	
	}
}
